<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="医院名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['hospitalName']" placeholder="请输入医院名称"></a-input>
        </a-form-item>

        <a-form-item label="医院类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['hospitalType']" placeholder="请输入医院类型"></a-input>
        </a-form-item>

        <a-form-item label="医院等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['hospitalGrade']" placeholder="请输入医院等级"></a-input>
        </a-form-item>
        <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload text="图片"
                    :number="1"
                    v-decorator="['images']"
                    fileType="image"/>
        </a-form-item>
        <a-form-item label="图标" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload text="图标"
                    :number="1"
                    v-decorator="['icon']"
                    fileType="image"/>
        </a-form-item>
        <a-form-item label="医院简介" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['hospitalSynopsis']" rows="4" placeholder="请输入医院简介"/>
        </a-form-item>
        <a-form-item label="医院详情" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-editor v-decorator="['hospitalDetail']"
                    triggerChange
                    placeholder="请输入内容"/>
        </a-form-item>

        <a-form-item label="医院地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          省：
          <a-select v-model="model.provinceId"
                    placeholder="请选择省份" @change="getListByPidAndGrade(2)" style="width: 100px">
            <a-select-option v-for="d in provinceList"
                             :key="d.id"
                             :value="d.id">{{ d.regionName }}
            </a-select-option>
          </a-select>
          市：
          <a-select v-model="model.cityId"
                    placeholder="请选择城市" @change="getListByPidAndGrade(3)" style="width: 100px">
            <a-select-option v-for="d in cityList"
                             :key="d.id"
                             :value="d.id">{{ d.regionName }}
            </a-select-option>
          </a-select>
          区：
          <a-select v-model="model.areaId"
                    placeholder="请选择区县" style="width: 100px">
            <a-select-option v-for="d in areaList"
                             :key="d.id"
                             :value="d.id">{{ d.regionName }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="医院详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['hospitalAddress']" placeholder="请输入医院地址"/>
        </a-form-item>
        <a-form-item label="地图标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          经度：
          <a-input v-decorator="['longitude']" placeholder="请输入经度" style="width: 100px"/>
          纬度：
          <a-input v-decorator="['latitude']" placeholder="请输入纬度" style="width: 100px"/>
        </a-form-item>
        <a-form-item label="医院标签" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <template v-for="(tag, index) in tags">
            <a-tooltip v-if="tag.length > 20" :key="tag" :title="tag">
              <a-tag :key="tag.id" :closable="index !== 0" @close="() => handleClose(tag)">
                {{ `${tag.slice(0, 20)}...` }}
              </a-tag>
            </a-tooltip>
            <a-tag v-else :key="tag.id" :closable="index !== 0" @close="() => handleClose(tag)">
              {{ tag.tag }}
            </a-tag>
          </template>
          <a-input
            v-if="inputVisible"
            ref="input"
            type="text"
            size="small"
            :style="{ width: '78px' }"
            :value="inputValue"
            @change="handleInputChange"
            @blur="handleInputConfirm"
            @keyup.enter="handleInputConfirm"
          />
          <a-tag v-else style="background: #fff; borderStyle: dashed;" @click="showInput">
            <a-icon type="plus"/>
            添加标签
          </a-tag>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JEditor from '@/components/jeecg/JEditor'
  import JUpload from '@/components/jeecg/JUpload'


  export default {
    name: "MedicalHospitalInfoModal",
    components: {
      JDate,
      JEditor,
      JUpload,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          add: "/hospital/medicalHospitalInfo/add",
          edit: "/hospital/medicalHospitalInfo/edit",
          getListByPidAndGrade: "/hospital/smsRegionInfo/getListByPidAndGrade",
        },
        tags: [],
        inputVisible: false,
        inputValue: '',
        provinceList: [],
        cityList: [],
        areaList: [],
      }
    },
    created() {
    },
    mounted() {
      this.initIndex();
    },
    methods: {
      initIndex() {
        getAction(this.url.getListByPidAndGrade, {grade: 1}).then((res) => {
          if (res.success) {
            this.provinceList = res.result;
          }
        });
        if (this.model.provinceId != null) {
          getAction(this.url.getListByPidAndGrade, {pid: this.model.provinceId, grade: 2}).then((res) => {
            if (res.success) {
              this.cityList = res.result;
            }
          });
        }
        if (this.model.cityId != null) {
          getAction(this.url.getListByPidAndGrade, {pid: this.model.cityId, grade: 3}).then((res) => {
            if (res.success) {
              this.areaList = res.result;
            }
          });
        }
      },
      getListByPidAndGrade(grade) {
        console.info(this.model.provinceId)
        if (grade == 2) {
          getAction(this.url.getListByPidAndGrade, {pid: this.model.provinceId, grade: 2}).then((res) => {
            if (res.success) {
              this.cityList = res.result;
            }
          });
        } else {
          getAction(this.url.getListByPidAndGrade, {pid: this.model.cityId, grade: 3}).then((res) => {
            if (res.success) {
              this.areaList = res.result;
            }
          });
        }
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.cityList = [];
        this.areaList = [];
        this.tags = [];
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'hospitalName', 'images', 'icon', 'hospitalSynopsis', 'hospitalAddress', 'hospitalGrade', 'hospitalType', 'hospitalLabels', 'provinceId', 'cityId', 'areaId', 'longitude', 'latitude', 'hospitalDetail'))
        })
        if (this.model.hospitalLabels != null) {
          this.tags = this.model.hospitalLabels;
        }
        if (this.model.provinceId != null) {
          this.initIndex();
        }
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            this.model.hospitalLabels = this.tags;
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'hospitalName', 'images', 'icon', 'hospitalSynopsis', 'hospitalAddress', 'hospitalGrade', 'hospitalType', 'hospitalLabels', 'provinceId', 'cityId', 'areaId'))
      },


      handleClose(removedTag) {
        const tags = this.tags.filter(tag => tag !== removedTag);
        this.tags = tags;
      },

      showInput() {
        this.inputVisible = true;
        this.$nextTick(function () {
          this.$refs.input.focus();
        });
      },

      handleInputChange(e) {
        this.inputValue = e.target.value;
      },

      handleInputConfirm() {
        const inputValue = this.inputValue;
        let tags = this.tags;
        if (inputValue && tags.indexOf(inputValue) === -1) {
          tags = [...tags, {tag: inputValue}];
        }
        Object.assign(this, {
          tags,
          inputVisible: false,
          inputValue: '',
        });
      },
    }
  }
</script>